package com.leixiaoqiao.controller;

import com.leixiaoqiao.pojo.Admin;
import com.leixiaoqiao.service.impl.AdminServiceImpl;
import com.leixiaoqiao.util.VerificationCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/admin")
public class AdminController extends BaseServlet {
    //登录功能
    public void login(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //获取前端发送的登录信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Admin admin = new Admin(username, password);
        //获取验证码信息
        String captcha = req.getParameter("captcha");
        //由于前端已经设置了非空，所以后端就没校验前端参数是否非空

        //获取会话
        HttpSession session = req.getSession();
        //先从域中拿到随机产生的验证码
        Object randCheckCode = session.getAttribute("randCheckCode");
        //对比用户输入的验证码是否有效
        if (!captcha.equalsIgnoreCase(randCheckCode+"")){
            String msg="验证码错误！哈ber";
            //将前后端交互信息放到域中
            req.setAttribute("msg",msg);
            //转发回登陆页面
            req.getRequestDispatcher("/page/login.jsp").forward(req,resp);
            return;//结束本次方法
        }
        //调用业务层的方法判断是否用户信息是否合法
        //首先创建业务层的实例
        AdminServiceImpl adminService = new AdminServiceImpl();
        if (adminService.verifyAdmin(admin)){
            //登陆成功
            String msg="登陆成功";
            //将前后端交互信息放到域中
            req.setAttribute("msg",msg);
            //看用户是否需要记住用户名和密码
            String rememberMe = req.getParameter("rememberMe");
            if ("true".equals(rememberMe)){
                //需要记住用户信息，则将其放入cookie中
                Cookie rName = new Cookie("rName", username);
                Cookie rPsw = new Cookie("rPsw", password);
                //setPath(String path):设置cookie的获取范围
                rName.setPath(req.getContextPath());//设置范围在此项目下
                rPsw.setPath(req.getContextPath());
                //将Cookie数据写到硬盘的文件中，持久化存储。并指定cookie存活时间，时间到后，cookie文件自动失效
                rName.setMaxAge(60*60*24*2);//两天
                rPsw.setMaxAge(60*60*24*2);
                //发送Cookie对象
                resp.addCookie(rName);
                resp.addCookie(rPsw);
            }
            //使用session记录登录成功凭据
            session.setAttribute("successAdmin",admin);
            //重新向后端发送显示数据的请求
            resp.sendRedirect(req.getContextPath()+"/customer?method=showList");
        }else{
            //登陆失败
            String msg="用户名或密码错误！";
            //将前后端交互信息放到域中
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("/page/login.jsp").forward(req,resp);
        }
    }
    //登出功能
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //让当前会话失效
        HttpSession session = req.getSession();
        session.invalidate();
        resp.sendRedirect(req.getContextPath()+"/page/login.jsp");
    }

    //响应验证码放到会话域中，并写给前端
    public void getAuthCode(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        VerificationCodeUtil.pictureCheckCode(req,resp);
    }
}
